--
-- Author: shunguo.chen
-- Date: 2017-12-28 15:12:16
--
-- PvpWinView pvp胜利界面

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpWinScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			self:closeView()
		end)
end

-- data = {PvpRewardVo, ...}
function ClassRef:refresh(data, reason, playId, dailyScore, winTimes)
	local panel = self._ui.Panel

	if reason then
		-- 胜利不显示reason了
		-- panel:getChildByName("tlt_1"):setString(reason)
		-- ccui.Helper:doLayout(panel)
	end

	local len = #data
    local tempVisible = {}
	for i = 1, 3 do
		self._ui["icon_" .. i]:setVisible(false)
    end

	if len == 2 then
		local width = panel:getContentSize2()
		local iconWidth = self._ui.icon_1:getChildByName("Panel"):getContentSize2()
		self._ui.icon_2:setPositionX(width*0.35 - iconWidth/2)
		self._ui.icon_3:setPositionX(width*0.65 - iconWidth/2)
        tempVisible = {2, 3}
	elseif len == 1 then
		tempVisible = {2}
	elseif len == 0 then

    else
        tempVisible = {1, 2, 3}
    end

    for _, v in ipairs(tempVisible) do
        self._ui["icon_" .. v]:setVisible(true)
    end

	for i,v in ipairs(data) do
		local icon = self._ui["icon_" .. tempVisible[i]]
		local text = icon:getChildByName("Text")
		text:setVisible(v.isFirstWin or v.isBoost)

		local render = IconRender.create(icon)
		render:setData(v.itemData)
		render:setTipEnable(true)
		render:setTipUnShowOrigin(true)
	end

	-- 挑战赛星章数上限，胜场数上限
	if playId and dailyScore and winTimes then
		local config = GD:getChallengeInfo(playId)
		local maxWinTimes = config and tonumber(config.winNumMax) or 0
		local maxScore = tonumber( GD:getSystemPar( "challengeGameMaxDailyBadge", 100) )
		local allFlag = (dailyScore >= maxScore and winTimes >= maxWinTimes) or false
		local starCountFlag = dailyScore >= maxScore or false
		if allFlag then
			self._ui.text:setString(L("challengeTips20"))
		elseif starCountFlag then
			self._ui.text:setString(L("challengeTips19"))
		end
		self._ui.tips_challenge:setVisible(len == 0 and (allFlag or starCountFlag))
		self._ui.tips_img:setVisible(not (allFlag or starCountFlag))
		self._ui.tips_1:setVisible(not (allFlag or starCountFlag))
	end

	-- 跨服精英赛光铸金币和经验达到上限
	if playId and (playId == Enums.TeamTarget.eliteMatchArena or playId == Enums.TeamTarget.eliteMatchDuel) then
		if len <= 0 then
			self._ui.tips_challenge:setVisible(true)
			self._ui.tips_img:setVisible(false)
			self._ui.tips_1:setVisible(false)
			self._ui.text:setString(L("EliteMatchTip53"))
		end
	end
end

return ClassRef
